package com.acane.instagram.service.impl;

import com.acane.instagram.mapper.CommentMapper;
import com.acane.instagram.pojo.Result;
import com.acane.instagram.pojo.dto.CommentDTO;
import com.acane.instagram.pojo.dto.CommentLikeDTO;
import com.acane.instagram.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentDTO> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public Result getCommentHistory(String userId) {
        List<CommentDTO> commentDTOList = commentMapper.getCommentHistory(userId);
        return Result.success(commentDTOList);
    }

    @Override
    public Result getCommentList(String postId) {
        List<CommentDTO> commentDTOList = commentMapper.getCommentList(postId);
        return Result.success(commentDTOList);
    }

    @Override
    public int likeComment(String commentId) {
        return commentMapper.likeComment(commentId);
    }

    @Override
    public int unlikeComment(String commentId) {
        return commentMapper.unlikeComment(commentId);
    }

    @Override
    public Result getCommentLikeHistory(String userId) {
        List<CommentLikeDTO> commentLikeDTOList = commentMapper.getCommentLikeHistory(userId);
        return Result.success(commentLikeDTOList);
    }

    @Override
    public boolean judgeIfAlreadyLike(String commentId, String likerId) {
        CommentLikeDTO commentLikeDTO = commentMapper.judgeIfAlreadyLike(commentId, likerId);
        return commentLikeDTO != null;
    }

    @Override
    public int countByPostId(String postId) {
        return commentMapper.countByPostId(postId);
    }

    @Override
    public Result getAllComment(
            Integer page,
            Integer size,
            String commenterId,
            String content,
            LocalDate startDate,
            LocalDate endDate
    ) {
        // 计算分页偏移量
        int offset = (page - 1) * size;
        List<CommentDTO> commentDTOList = commentMapper.getAllComment(
                commenterId,
                content,
                startDate,
                endDate,
                offset,
                size
        );
        if (!commentDTOList.isEmpty()) {
            return Result.success(commentDTOList, commentDTOList.size());
        }
        return Result.failure(0, "暂无评论");
    }


}
